package com.luzhikun.Listener.sessionBindingListener;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.Objects;

/**
 * @Author Rookie Coder
 * @Description 八大监听器之HttpSessionBindingListener
 * @Date 实体类实现 HttpSessionBindingListener监听器接口
 * 事件源是实体类
 * 该监听器是不用注册的
 **/
public class HttpSessionBinDingTest implements HttpSessionBindingListener {

    private String name;
    private int age;


    public HttpSessionBinDingTest() {
    }

    public HttpSessionBinDingTest(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof HttpSessionBinDingTest)) return false;
        HttpSessionBinDingTest that = (HttpSessionBinDingTest) o;
        return age == that.age && Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "HttpSessionBinDingTest{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    // 监听是否向域中添加属性
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println(event.getName() + "---" + event.getValue());
    }

    // 监听是否向域中删除属性
    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.out.println(event.getName() + "---" + event.getValue());
    }
}
